package com.net;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.net.callback.TrustAllCerts;
import com.substation.utils.SPUtils;
import com.tamic.novate.Novate;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import dmax.dialog.SpotsDialog;

/**
 * Created by admin on 2018/1/29.
 */

public class BaseNet {

    public static SpotsDialog d;

    protected static final int limit = 15;

    /**
     * 获取网络请求实例
     *
     * @param context
     * @return
     */
    protected static Novate getNovate(Context context) {
        return new Novate.Builder(context).baseUrl(Contants.getBaseUrl(context)).addHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        }).connectTimeout(30).readTimeout(30).addSSLSocketFactory(createSSLSocketFactory()).addLog(true).addHeader(getHead(context)).build();
    }

    protected static Novate getNovateOfDialog(Context context, String msg) {
        getDialog(context, msg);
        return new Novate.Builder(context).baseUrl(Contants.getBaseUrl(context)).addHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        }).connectTimeout(30).readTimeout(30).addSSLSocketFactory(createSSLSocketFactory()).addLog(true).addHeader(getHead(context)).build();
    }


    protected static SpotsDialog getDialog(Context context, String msg) {
        if (d != null) {
            d.dismiss();
            d.cancel();
            d = null;
        }
        d = new SpotsDialog(context, msg);
        d.show();
        d.setCancelable(false);
        return d;
    }

    /**
     * 统一获取请求头部
     *
     * @param context
     * @return
     */
    protected static Map<String, String> getHead(Context context) {
        Map<String, String> headers = new HashMap<>();
        String token = (String) SPUtils.get(context, "token", "");
        if (token != null && !token.equals("")) {
            headers.put("token", token);
        }
        headers.put("Content-Type", "application/json;charset=utf-8");
        headers.put("version", "1.0");
        headers.put("deviceId", getDeviceId(context));
        return headers;
    }

    protected static String getDeviceId(Context context) {
        return ((TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    protected static Map<String, Object> getParams(Context context) {
        Map<String, Object> map = new HashMap<>();
        String token = (String) SPUtils.get(context, "token", "");
        if (!token.equals("")) {
            map.put("token", token);
        }
        return map;
    }

    protected static SSLSocketFactory createSSLSocketFactory() {
        SSLSocketFactory ssfFactory = null;

        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());
            ssfFactory = sc.getSocketFactory();
        } catch (Exception e) {
        }

        return ssfFactory;
    }
}
